package com.hksj.cm.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hksj.cbm.mapper.DictionaryMapper;
import com.hksj.cbm.vo.DictionaryItemVO;
import com.hksj.cm.dto.DeviceLevelSettingDTO;
import com.hksj.cm.entity.DeviceLevelSetting;
import com.hksj.cm.mapper.DeviceLevelMapper;
import com.hksj.cm.vo.ConstructionStandardsVO;
import com.hksj.cm.vo.DeviceLevelVO;
import com.hksj.common.core.model.Rest;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName DeviceLevelService
 * @Description TODO
 * @Author @陈章
 * @Date 2024/7/22 10:37
 * @Version 1.0
 */
@Service
public class DeviceLevelService extends ServiceImpl<DeviceLevelMapper, DeviceLevelSetting> {
    @Autowired
    private DeviceLevelMapper mapper;

    @Autowired
    private DictionaryMapper dictionaryMapper;


    public List<ConstructionStandardsVO> selectConstructionStandardsList() {
        List<DictionaryItemVO> items = dictionaryMapper.selectByDictId("constructionStandards");
        List<ConstructionStandardsVO> list = new ArrayList<>();
        for (DictionaryItemVO item : items) {
            ConstructionStandardsVO vo = new ConstructionStandardsVO();
            String value = item.getValue();
            int count = mapper.selectCountByValue(value);
            vo.setConstructionStandardsValue(item.getValue());
            vo.setConstructionStandardsName(item.getName());
            vo.setCount(count);
            list.add(vo);
        }
        return list;
    }

    public Page<DeviceLevelVO> queryUnboundDeviceList(Integer pageSize, Integer pageNum, String deviceName, String constructionStandardsValue) {
        Page<DeviceLevelVO> page = new Page<>(pageNum, pageSize);
        List<DeviceLevelVO> list = mapper.queryUnboundDeviceList(page, deviceName, constructionStandardsValue);
        page.setRecords(list);
        return page;
    }

    public Page<DeviceLevelVO> queryAlreadyBoundDeviceList(Integer pageSize, Integer pageNum, String constructionStandardsValue) {
        Page<DeviceLevelVO> page = new Page<>(pageNum, pageSize);
        List<DeviceLevelVO> list = mapper.queryAlreadyBoundDeviceList(page, constructionStandardsValue);
        page.setRecords(list);
        return page;
    }


    public Rest<String> createDeviceLevelSetting(List<DeviceLevelSettingDTO> list) {
        if (CollectionUtils.isEmpty(list)) {
            Rest.fail("数据不能为空");
        }
        for (DeviceLevelSettingDTO deviceLevelSettingDTO : list) {
            DeviceLevelSetting deviceLevelSetting = new DeviceLevelSetting();
            BeanUtils.copyProperties(deviceLevelSettingDTO, deviceLevelSetting);
            if (deviceLevelSettingDTO.getId() != null) {
                mapper.updateById(deviceLevelSetting);
            } else {
                mapper.insert(deviceLevelSetting);
            }
        }
        return Rest.ok("添加成功");
    }

    public Rest<String> delDeviceLevelSetting(Long id) {
        int i = mapper.deleteById(id);
        if (i == 0) {
            Rest.fail("删除失败");
        }
        return Rest.ok("删除成功");
    }
}
